import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 涛
 * Date: 2023-10-03
 * Time: 10:04
 */
public class MyLinkList implements ILinkList{

    static class ListNode {
        int val;

        ListNode next;//后继
        ListNode prev;//前驱
        public ListNode(int val) {
            this.val = val;
        }
    }
    ListNode head;//头结点
    ListNode last;//尾结点

    @Override
    public void addFirst(int data) {
        ListNode cur = new ListNode(data);
        //没有节点
        if(this.head == null) {
            this.head = cur;
        } else {
            //头插
            cur.next = this.head;
            this.head.prev = cur;
            this.head = cur;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode cur = new ListNode(data);
        ListNode node = head;
        //没有节点
        if(this.head == null) {
            this.head = cur;
        } else {
            //找到尾结点
            while(node.next != null) {
                node = node.next;
            }
            //尾插
            node.next = cur;
            cur.prev = node;
        }

    }

    @Override
    public void addIndex(int index, int data) {
        //判断index 的合法性
        if(index < 0 ||index > size()) {
            //抛异常
            throw new MyLinkLIstException("插入下标不合法");
        }
        //inde == 0，头插
        if(index == 0) {
            addFirst(data);
            return;
        }
        //index == size，尾插
        if(index == size()) {
            addLast(data);
            return;
        }
        //有节点
        ListNode node = new ListNode(data);
        ListNode curBefore = head;

        //找到前驱curBefore,和index位置的cur
        for(int i = 0; i < index - 1; i++) {
            curBefore = curBefore.next;
        }
        ListNode cur = curBefore.next;
        curBefore.next = node;
        node.prev = curBefore;
        node.next = cur;
        cur.prev = node;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        if(this.head == null) {
            //抛异常
            throw new MyLinkLIstException("链表一个节点都没有");
        } else {

            while(cur != null) {
                if(cur.val == key) {
                   return true;
                }
                cur = cur.next;
            }
        }
        return false;
    }

    @Override
    public void remove(int key) {
        //没有节点
        if(this.head == null) {
            return;
        }
        ListNode node = this.head;
        ListNode curBefore = this.head;
        ListNode cur = curBefore.next;
        while(node != null) {
            if(cur.val == key) {
                curBefore.next = cur.next;
                if(cur.next != null) {
                    cur.next.prev = curBefore;
                } else {
                    curBefore.next = null;
                }
                break;
            } else {
                curBefore = curBefore.next;
                cur = cur.next;
            }
            node = node.next;
        }
        //判断头结点有没有关键字
        if(this.head.val == key) {
            this.head = this.head.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
//没有节点
        if(this.head == null) {
            return;
        }
        ListNode node = this.head;
        ListNode curBefore = this.head;
        ListNode cur = curBefore.next;
        while(node.next != null) {
            if(cur.val == key) {
                curBefore.next = cur.next;
                if(cur.next != null) {
                    cur.next.prev = curBefore;
                } else {
                    curBefore.next = null;
                }
                cur = cur.next;
            } else {
                curBefore = curBefore.next;
                cur = cur.next;
            }
            node = node.next;
        }
        //判断头结点有没有关键字
        if(this.head.val == key) {
            this.head = this.head.next;
        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode node = head;
        while(node != null) {
            count++;
            node = node.next;
        }
        return count;
    }

    @Override
    public void display() {
        ListNode node = this.head;
        while(node != null) {
            System.out.println(node.val + " ");
            node = node.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curNext;
        }
        head = null;
    }
}
